/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.game.room.handler;

import com.smartfoxserver.v2.annotations.MultiHandler;
import com.smartfoxserver.v2.db.IDBManager;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSArray;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSArray;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import com.smartfoxserver.v2.extensions.SFSExtension;
import extension.game.room.RoomExtension;
import extension.game.shop.Item;
import extension.game.shop.Shop;
import extension.game.shop.TypeItem;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Hung
 */
@MultiHandler
public class ShopHandler extends BaseClientRequestHandler {

    public static final String ALL = "_all_";
    Shop shop;
    RoomExtension roomExtension;

    @Override
    public void handleClientRequest(User user, ISFSObject params) {
        String requestValue = params.getUtfString(SFSExtension.MULTIHANDLER_REQUEST_ID);
        roomExtension = ((RoomExtension) getParentExtension());
        shop = roomExtension.getShop();

        if (requestValue.equalsIgnoreCase("INFO")) {
            onShopInfo(user, params);
        } else if (requestValue.equalsIgnoreCase("BUY")) {
            onBuyItem(user, params);
        }
    }

    private void onShopInfo(User user, ISFSObject params) {
        ISFSObject respond = new SFSObject();
        ISFSArray itemArray = new SFSArray();
        ISFSArray typeArray = new SFSArray();
        TypeItem[] types = shop.getTypeData();
        Item[] items = shop.getItemData();
        for (int i = 0; i < types.length; i++) {
            typeArray.addSFSObject(types[i].toSFSObject());
        }
        for (int i = 0; i < items.length; i++) {
            itemArray.addSFSObject(items[i].toSFSObject());
        }
        respond.putBool("result", true);
        respond.putSFSArray("type", typeArray);
        respond.putSFSArray("item", itemArray);
        respond.putInt("total", itemArray.size());
        send(RoomCommand.SHOP_INFO, respond, user);
    }

    private void onBuyItem(User user, ISFSObject params) {
        ISFSObject respond = new SFSObject();
        int id = params.getInt("itemId");
        String[] receiveItem = params.getUtfString("userReceive").split(",");
        List<String> receiveItemRes = new ArrayList<String>();
        if (receiveItem != null && receiveItem.length > 0) {
            List<User> list = roomExtension.getParentRoom().getPlayersList();
            if (receiveItem[0].equals(ALL)) {
                for (int i = 0; i < list.size(); i++) {
                    receiveItemRes.add(list.get(i).getName());
                }
            } else {
                for (int i = 0; i < receiveItem.length; i++) {
                    receiveItemRes.add(receiveItem[i]);
                }
            }
        }
        if (receiveItemRes != null && shop.buyItem(id, receiveItemRes.size(), user) == Shop.SUCCESS) {
            String url = "";
            for (int i = 0; i < shop.getItemData().length; i++) {
                if (shop.getItemData()[i].getId() == id) {
                    url = shop.getItemData()[i].getImage();
                    break;
                }
            }
            IDBManager dbManager = getParentExtension().getParentZone().getDBManager();
            String sql = "SELECT money FROM " + Shop.moneyQueueTableName + " WHERE username = ?";
            try {
                ISFSArray rows = dbManager.executeQuery(sql, new Object[]{user.getName()});
                if (rows.size() > 0) {
                    respond.putLong("updateMoney", rows.getSFSObject(0).getLong("money"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            respond.putBool("result", true);
            respond.putInt("itemId", id);
            respond.putUtfString("itemImage", url);
            respond.putUtfString("userBuy", user.getName());
            respond.putUtfStringArray("userReceive", receiveItemRes);
            send(RoomCommand.SHOP_BUY, respond, getParentExtension().getParentRoom().getUserList());
            return;
        }
        if (receiveItem != null && shop.buyItem(id, receiveItemRes.size(), user) == Shop.NOT_MONEY) {
            respond.putBool("result", false);
            respond.putUtfString("message", "Bạn không đủ tiền");
            send(RoomCommand.SHOP_BUY, respond, user);
            return;
        }

        respond.putBool("result", false);
        respond.putUtfString("message", "Thao tác không hợp lệ");
        send(RoomCommand.SHOP_BUY, respond, user);
    }
}
